
import 'package:DiamondDiary/database/note_info.dart';
import 'package:DiamondDiary/utils/string_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NotePreviewPage extends StatefulWidget{

  int id;
  NoteInfo info;
  NotePreviewPage({this.id, this.info});

  @override
  State<StatefulWidget> createState() {
    return NotePreviewState();
  }

}

class NotePreviewState extends State<NotePreviewPage>{

  var tagList = <Map<String,dynamic>>[];
  var set = Set<String>();

  @override
  void initState() {
    if(widget.info.tags!='null'){
      if(widget.info.tags.contains(', ')){
        set.addAll(widget.info.tags.split(', '));
      }else{
        set.add(widget.info.tags);
      }
    }
    super.initState();
    // Future.delayed(Duration(milliseconds: 100),()async{
    //   var tags =await DBHelper().getTagInfo(limit: 10);
    //   if(tags!=null && tags.length>0){
    //     setState(() {
    //       tagList.addAll(tags);
    //     });
    //   }
    // });
  }

  @override
  void dispose() {
    super.dispose();
  }



  @override
  Widget build(BuildContext context) {
    return
      Scaffold(
        appBar: AppBar(title: Center(child:Text(''),),actions: [
          InkWell(
            child:Padding(
              padding: EdgeInsets.only(left:10,right: 10),
              child: Center(child:Text('')),
            ),
            onTap: (){},),
        ],),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              width: double.infinity,
              margin: EdgeInsets.all(15),
              padding: EdgeInsets.only(left: 10,right: 10,top: 5,bottom: 5),
              decoration: ShapeDecoration(
                color:Colors.white,
                shape:RoundedRectangleBorder(borderRadius:BorderRadius.all(Radius.circular(5))),
              ),
              child:Text(widget.info.title,
                style: Theme.of(context)
                    .textTheme
                    .headline6
                    .copyWith(color: Colors.black87),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 18,bottom: 15),
              child: Text(StringUtils.formatTime(widget.info.commitTime)),
            ),
            tagList.length>0?buildTags():SizedBox(height: 1,),
            Text(set.toString()),//TODO  显示 tag
            Expanded(
              child: Container(
                width: double.infinity,
                margin: EdgeInsets.fromLTRB(15, 0, 15, 15),
                padding: EdgeInsets.only(left: 10,right: 10),
                decoration:ShapeDecoration(
                  color:Colors.white,
                  shape:RoundedRectangleBorder(borderRadius:BorderRadius.all(Radius.circular(5))),
                ),
                child: SingleChildScrollView(
                  child:Text(widget.info.content,
                    style:TextStyle(fontSize: 16,height: 1.6),
                  ),
                ),
              ),
            ),
          ],
        ),
      );
  }

  Widget buildTags(){
    return Container(
      height: 30,
      child:ListView.separated(
        scrollDirection : Axis.horizontal,
        shrinkWrap: true,
        padding: EdgeInsets.only(left: 10,right: 10),
        itemBuilder: (context,index){
          var item = tagList[index];
          return Container(
            alignment: Alignment.center,
            padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
            decoration:ShapeDecoration(
              color:Colors.black26,
              shape:RoundedRectangleBorder(borderRadius:BorderRadius.all(Radius.circular(5))),
            ) ,
            child: Text(item['tag']?.toString()??'null'),
          );
        },
        separatorBuilder: (context,index)=>SizedBox(width: 10,),
        itemCount: tagList.length),
    );
  }

}